﻿using RpcClient.Interface;
using RpcHelper;
using RpcHelper.Error;
using RpcModel;
using RpcSync.Collect.linq;
using RpcSync.Collect.Model;
using RpcSync.DAL;
using RpcSync.Model.DB;

namespace RpcSync.Collect.Collect
{
    [RpcClient.Attr.ClassLifetimeAttr(RpcClient.Attr.ClassLifetimeType.单例)]
    internal class BroadcastErrorCollect : IBroadcastErrorCollect
    {
        private readonly DelayDataSave<BroadcastErrorLogModel> _SaveErrorLog = null;
        private IUnityCollect _Unity;
        private static readonly long _DeadErrorId = 0;
        static BroadcastErrorCollect()
        {
            _DeadErrorId = LocalErrorManage.GetErrorId("rpc.queue.dead.msg");
        }
        public BroadcastErrorCollect(IUnityCollect unity)
        {
            _Unity = unity;
            _SaveErrorLog = new DelayDataSave<BroadcastErrorLogModel>(new SaveDelayData<BroadcastErrorLogModel>(_SaveLog), Tools.GetRandom(120, 180), 15);
        }
        private void _SaveLog(ref BroadcastErrorLogModel[] datas)
        {
            IBroadcastErrorLogDAL logDAL = _Unity.Resolve<IBroadcastErrorLogDAL>();
            logDAL.AddErrorLog(datas);
        }
       
        public void AddErrorLog(QueueRemoteMsg msg, string routeKey, BroadcastType msgType)
        {
            MsgSource source = msg.Msg.Source;
            _SaveErrorLog.AddData(new BroadcastErrorLogModel
            {
                Id = Tools.NewGuid(),
                RpcMerId = source.RpcMerId,
                ServerId = source.ServerId,
                BroadcastType = msgType,
                ErrorId = _DeadErrorId,
                RouteKey = routeKey,
                MsgKey = msg.Type,
                SysTypeVal = string.Empty,
                MsgSource = source.ToJson(),
                MsgBody = msg.Msg.ToMsgBody(),
                AddTime = DateTime.Now
            });
        }

        public void AddErrorLog(RemoteBroadcast data, string error)
        {
            _SaveErrorLog.AddData(new BroadcastErrorLogModel
            {
                Id = Tools.NewGuid(),
                RpcMerId = data.Msg.RpcMerId,
                BroadcastType = BroadcastType.默认,
                ErrorId = LocalErrorManage.GetErrorId(error),
                MsgKey = data.Msg.Datum.MsgKey,
                ServerId = data.ServerId,
                SysTypeVal = data.TypeVal ?? string.Empty,
                MsgSource = data.Msg.Source.ToJson(),
                MsgBody = data.Msg.Datum.MsgBody,
                AddTime = DateTime.Now
            });
        }


        public void AddErrorLog(BroadcastBody msg, string error)
        {
            if (!msg.ServerId.IsNull())
            {
                _AddErrorLog(msg, msg.ServerId, error);
            }
            else if (!msg.Dictate.IsNull())
            {
                _AddErrorLog(msg, msg.Dictate, error);
            }
            else
            {
                _AddErrorLog(msg, error);
            }
        }
        private void _AddErrorLog(BroadcastBody msg, string error)
        {
            _SaveErrorLog.AddData(new BroadcastErrorLogModel
            {
                Id = Tools.NewGuid(),
                RpcMerId = msg.RpcMerId,
                BroadcastType = BroadcastType.默认,
                ErrorId = LocalErrorManage.GetErrorId(error),
                MsgKey = msg.Datum.MsgKey,
                MsgSource = msg.Source.ToJson(),
                MsgBody = msg.Datum.MsgBody,
                AddTime = DateTime.Now
            });
        }
        private void _AddErrorLog(BroadcastBody msg, string[] types, string error)
        {
            types.ForEach(a =>
            {
                _SaveErrorLog.AddData(new BroadcastErrorLogModel
                {
                    Id = Tools.NewGuid(),
                    RpcMerId = msg.RpcMerId,
                    BroadcastType = BroadcastType.默认,
                    ErrorId = LocalErrorManage.GetErrorId(error),
                    MsgKey = msg.Datum.MsgKey,
                    SysTypeVal = a,
                    MsgSource = msg.Source.ToJson(),
                    MsgBody = msg.Datum.MsgBody,
                    AddTime = DateTime.Now
                });
            });
        }
        private void _AddErrorLog(BroadcastBody msg, long[] serverId, string error)
        {
            serverId.ForEach(a =>
            {
                _SaveErrorLog.AddData(new BroadcastErrorLogModel
                {
                    Id = Tools.NewGuid(),
                    RpcMerId = msg.RpcMerId,
                    BroadcastType = BroadcastType.默认,
                    ErrorId = LocalErrorManage.GetErrorId(error),
                    MsgKey = msg.Datum.MsgKey,
                    ServerId = a,
                    MsgSource = msg.Source.ToJson(),
                    MsgBody = msg.Datum.MsgBody,
                    AddTime = DateTime.Now
                });
            });
        }




        public void AddErrorLog(BroadcastMsg msg, MsgSource source, string error)
        {
            _SaveErrorLog.AddData(new BroadcastErrorLogModel
            {
                Id = Tools.NewGuid(),
                RpcMerId = msg.RpcMerId == 0 ? source.RpcMerId : msg.RpcMerId,
                ErrorId = LocalErrorManage.GetErrorId(error),
                MsgKey = msg.MsgKey,
                MsgSource = source.ToJson(),
                MsgBody = msg.ToJson(),
                AddTime = DateTime.Now
            });
        }

        public void AddErrorLog(long merId, BroadcastDatum msg, string typeVal, string error)
        {
            _SaveErrorLog.AddData(new BroadcastErrorLogModel
            {
                Id = Tools.NewGuid(),
                RpcMerId = merId,
                ErrorId = LocalErrorManage.GetErrorId(error),
                MsgKey = msg.MsgKey,
                ServerId = 0,
                SysTypeVal = typeVal,
                MsgBody = msg.ToJson(),
                AddTime = DateTime.Now
            });
        }
    }
}
